// created by noellynch
// May 9, 2010

package com.sceneworker.editors.inputcommands;

import static com.myjavautils.swing.SwingComponentFactory.makeACheckBox;
import static com.myjavautils.swing.SwingComponentFactory.makeAComboBox;
import static com.myjavautils.swing.SwingComponentFactory.makeALabel;
import static com.myjavautils.swing.SwingComponentFactory.makeAList;

import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.miginfocom.swing.MigLayout;

import org.lwjgl.input.Keyboard;

import com.jme.input.KeyInput;
import com.jme.input.KeyInputListener;
import com.jme.input.MouseInput;
import com.jme.input.MouseInputListener;
import com.jme2utils.input.CommandInputMapping;
import com.jme2utils.input.InputMappingTables;
import com.sceneworker.app.globals.SceneWorkerPreferencesPanel.IPrefsPanel;

public class InputCommandsPrefsPanel extends JPanel implements IPrefsPanel {
	
	private	JList		mcl_ipList;
	private	JList		mcl_keysList;
	private	JCheckBox	mcl_commEnabledBox;
	private	JComboBox	mcl_mouseCombo;
	
	public	InputCommandsPrefsPanel() {
		setLayout(new MigLayout("inset 5"));
		
		add(makeListPanel(), "width 50%, height 100%");
		add(makeCommandInfoPanel(), "width 50%, height 100%");
		
		mcl_ipList.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent arg0) {
			}
			
			public void keyReleased(KeyEvent arg0) {
			}
			
			public void keyPressed(KeyEvent cl_e) {
				System.out.println("from java char = " + cl_e.getKeyChar() + " : code = " + cl_e.getKeyCode() + " " + cl_e.getModifiersEx());
			}
		});
		
		KeyInput.get().addListener(new KeyInputListener() {
			public void onKey(char character, int keyCode, boolean pressed) {
				System.out.println("from key input char = " + character + " : code = " + keyCode + " : key rep " + KeyInput.get().getKeyName(keyCode) + " : pressed = " + pressed + ": evt char " + Keyboard.getEventCharacter());
			}
		});
		
		MouseInput.get().addListener(new MouseInputListener() {
			public void onWheel(int wheelDelta, int x, int y) {
			}
			
			public void onMove(int xDelta, int yDelta, int newX, int newY) {
			}
			
			public void onButton(int button, boolean pressed, int x, int y) {
				System.out.println("btn " + button + " " + MouseInput.get().getButtonName(button));
			}
		});
	}
	
	private	JPanel		makeListPanel() {
		
		mcl_ipList = makeAList();
		
		JPanel		lcl_p = new JPanel();
		lcl_p.setLayout(new MigLayout("inset 5"));
		
		JScrollPane		lcl_scroller = new JScrollPane();
		lcl_scroller.setViewportView(mcl_ipList);
		lcl_scroller.setMinimumSize(new Dimension(250, 250));
		lcl_p.add(lcl_scroller, "width 100%");
		
		ArrayList<CommandInputMapping>		lcla_comms = InputMappingTables.getDefaultMappingContext().getAllCommands();
		
		DefaultListModel		lcl_model = new DefaultListModel();
		
		for(CommandInputMapping lcl_comm : lcla_comms) {
			lcl_model.addElement(lcl_comm.getCommand());
		}
		
		mcl_ipList.setModel(lcl_model);
		
		mcl_ipList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				handleNewCommandListSelection();
			}
		});
		
		return lcl_p;
	}
	
	private		JPanel		makeCommandInfoPanel() {
		JPanel		lcl_p = new JPanel();
		lcl_p.setLayout(new MigLayout("inset 5"));
		
		lcl_p.add(makeALabel("Keys"), "wrap");
		
		mcl_keysList = makeAList();
		
		JScrollPane		lcl_scroller = new JScrollPane();
		lcl_scroller.setViewportView(mcl_keysList);
		lcl_scroller.setMinimumSize(new Dimension(80, 100));
		lcl_p.add(lcl_scroller, "width 50%");
		
		JPanel		lcl_info = new JPanel();
		lcl_info.setLayout(new MigLayout("inset 5"));
		
		mcl_commEnabledBox = makeACheckBox("Enabled", true);
		lcl_info.add(mcl_commEnabledBox, "wrap");
		
		mcl_mouseCombo = makeAComboBox();
		//mcl_mouseCombo.setModel(new DefaultComboBoxModel(new String[]{"None", "Left Mouse", "Right Mouse"}));
		
		int		li_nBtns = MouseInput.get().getButtonCount();
		
		DefaultComboBoxModel	lcl_m = new DefaultComboBoxModel();
		lcl_m.addElement("None");
		for(int li_btn = 0; li_btn < li_nBtns; li_btn++) {
			lcl_m.addElement(MouseInput.get().getButtonName(li_btn));
		}
		mcl_mouseCombo.setModel(lcl_m);
		
		lcl_info.add(mcl_mouseCombo, "wrap");
		
		lcl_p.add(lcl_info);
		
		return lcl_p;
	}
	
	private	void	handleNewCommandListSelection() {
		CommandInputMapping		lcl_comm = InputMappingTables.getDefaultMappingContext().getCommand((String)mcl_ipList.getSelectedValue());
		
		DefaultListModel		lcl_model = new DefaultListModel();
		
		for(Integer lcl_keyInt : lcl_comm.getKeys()) {
			lcl_model.addElement(KeyInput.get().getKeyName(lcl_keyInt));
		}
		
		mcl_commEnabledBox.setEnabled(lcl_comm.isCommandEnabled());
		
		mcl_keysList.setModel(lcl_model);
		
		if(lcl_comm.getBtnDown() == -1) {
			mcl_mouseCombo.setSelectedIndex(0);
		} else {
			mcl_mouseCombo.setSelectedItem(MouseInput.get().getButtonName(lcl_comm.getBtnDown()));
		}
		
	}
	
	public void apply() {
	}

	
}
